#include "widget.h"
#include "ui_widget.h"
#include "QMetaProperty"
#include <QDebug>
#include <QDateTime>
#include <QTimer>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->BoyAgeSpinBox->setProperty("isBoy", true);
    ui->GrilAgeSpinBox->setProperty("isBoy", false);

    boy = new QPerson("李嘉强");
    boy->setProperty("score", 95);
    boy->setProperty("age", 22);
    boy->setProperty("sex", "Boy");
    connect(boy, &QPerson::ageChanged, this, &Widget::on_ageChanged);

    girl = new QPerson("李慕岚");
    girl->setProperty("score", 95);
    girl->setProperty("age", 18);
    girl->setProperty("sex", "Girl");
    connect(girl, &QPerson::ageChanged, this, &Widget::on_ageChanged);
    connect(ui->BoyAgeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_spin_valueChanged(int)));
    connect(ui->GrilAgeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_spin_valueChanged(int)));

    QObjtectTimerId = -1;

    my_timer = new QTimer(this);
//    my_timer->setInterval(1000);
    connect(my_timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_spin_valueChanged(int arg)
{
    Q_UNUSED(arg);
    QSpinBox *spinBox = qobject_cast<QSpinBox*>(sender());
    if(spinBox->property("isBoy").toBool())
    {
        boy->setAge(spinBox->value());
    }
    else
    {
        girl->setAge(spinBox->value());
    }
}

void Widget::on_ageChanged(unsigned age)
{
    Q_UNUSED(age);
    QPerson * aPerson = qobject_cast<QPerson*>(sender());
    QString aName = aPerson->property("name").toString();
    QString aSex = aPerson->property("sex").toString();
    unsigned aAge = aPerson->age();

    ui->LogPlainTextEdit->appendPlainText(aName + " " + aSex + QString(" 年龄=%1").arg(aAge));
}

QString Widget::getDateTime()
{
    QDateTime *dateTime = new QDateTime(QDateTime::currentDateTime());
    QString dt = dateTime->toString("hh:mm:ss ddd");
    return dt;
}

void Widget::timerEvent(QTimerEvent *event)
{
    int id = event->timerId();

    if(id == QObjtectTimerId)
    {
        ui->label_timer01->setText(this->getDateTime());
    }

}

void Widget::on_BoyGrowUpBtn_clicked()
{
    boy->incAge();
}

void Widget::on_GrilGrowUpBtn_clicked()
{
    girl->incAge();
}

void Widget::on_ObjtectMateBtn_clicked()
{
    const QMetaObject *meta = girl->metaObject();
    ui->LogPlainTextEdit->clear();
    ui->LogPlainTextEdit->appendPlainText("==元对象信息=\n");
    ui->LogPlainTextEdit->appendPlainText(
                QString("类命名：%1\n").arg(meta->className()));
    ui->LogPlainTextEdit->appendPlainText("property");
    for(int i = meta->propertyOffset(); i<meta->propertyCount();i++)
    {
        QMetaProperty prop = meta->property(i);
        const char* propName = prop.name();
        QString propValue = boy->property(propName).toString();
        ui->LogPlainTextEdit->appendPlainText(QString("属性名称：%1, 属性值：%2").arg(propName).arg(propValue));
    }
}

void Widget::on_startTimer01Btn_clicked()
{
    if(QObjtectTimerId == -1)
    {
        QObjtectTimerId = this->startTimer(1000);

    }
}

void Widget::on_stopTimer01Btn_clicked()
{
    if(QObjtectTimerId != -1)
    {
        this->killTimer(QObjtectTimerId);
        QObjtectTimerId = -1;
    }
}

void Widget::on_reStartTimer01_clicked()
{
    if(QObjtectTimerId == -1)
    {
        ui->label_timer01->setText("00:00:00 周一 ");
        QObjtectTimerId = this->startTimer(1000);

    }
}

void Widget::on_startTimer02Btn_clicked()
{
    if(!my_timer->isActive())
    {
        my_timer->start(1000);
    }
}

void Widget::on_stopTimer02Btn_clicked()
{
    if(my_timer->isActive())
    {
        my_timer->stop();
    }
}

void Widget::on_reStartTimer02_clicked()
{
    if(!my_timer->isActive())
    {
        ui->label_timer02->setText("00:00:00 周一 ");
        my_timer->start(1000);
    }
}

void Widget::handleTimeout()
{
    ui->label_timer02->setText(this->getDateTime());
}
